// 定义json变量 
//var json ={username:"杨朝来",sex:1,age:25,birthday:"1990-08-21",phonenumber:13812345678,disease:"肺小结节",insurance:1,avatar:"images/male.png"}; 
// json变量转化成json字符串 
//var jsonstr =JSON.stringify(json); 
// 存储json字符串 
//window.localStorage.setItem("jsonkey",jsonstr); 
// 取出json字符串 
//var jsonstr =window.localStorage.getItem("jsonkey"); 
// 还原json对象 
////var json =JSON.parse(jsonstr); 
// 取出json里面的username 
///var username =json.username;



class COBJJSONWRIT {
	

	private static myself:COBJJSONWRIT
	private constructor() {
	}
	public static get getJSONWRIT():COBJJSONWRIT
	{
		if(this.myself==undefined)this.myself=new COBJJSONWRIT;
		return this.myself
	}
	public readnamespace(spacename:string)
	{
        let data=egret.localStorage.getItem("namespace");
		if(data==null)console.log("namespace还未创建空间");
		             
		else 
		{
			let datas=JSON.parse(data); 
			for(var a=0;a<datas.length;a++)
			{
				if(datas[a].Namespace==spacename)
				{
					console.log("namespace找到了")
				}
			}
		}
		console.log(data);
	}
	public writnamespace(spacename:string)
	{
        let data=egret.localStorage.getItem("namespace");
	
		if(data==null)
		{
				let  spacedata:{}[]=[]
	            let  namespacedata:{[index:string]:string}={}
			    namespacedata["Namespace"]=spacename;
			    spacedata.push(namespacedata);
			    egret.localStorage.setItem("namespace",JSON.stringify(spacedata));
		}
		else 
		{
			let datas=JSON.parse(data)
			for(var a=0;a<datas.length;a++)
			{
				if(datas[a].Namespace==spacename)
				{
					console.log("namespace重复了")
					return false;
				}
			}
			let  namespacedata:{[index:string]:string}={}
				 namespacedata["Namespace"]=spacename;
			     datas.push(namespacedata)
			egret.localStorage.removeItem("namespce");
			egret.localStorage.setItem("namespace",JSON.stringify(datas));

		}
	}
	public showallnamespace():any
	{
        let data=egret.localStorage.getItem("namespace");
	
		if(data!=null)
		{
			let datas=JSON.parse(data)
			return datas
		}
	}
	public checknamespace(spacename:string)
	{
       let data=egret.localStorage.getItem("namespace");
	
		if(data!=null)
		{
			let datas=JSON.parse(data)
			for(var a=0;a<datas.length;a++)
			{
				if(datas[a].Namespace==spacename)
				{
					console.log("namespace重复了")
					return false;
				}
			}
			console.log("namespace没有重复")
			return true;
		}
		else return true;
	
	}

	public deletedataobjdata(framename:string)
	{
		 egret.localStorage.removeItem(framename);
		 let data=egret.localStorage.getItem("namespace");
		 let datas=JSON.parse(data)
		 let bool=false;
			for(var a=0;a<datas.length;a++)
			{
				if(datas[a].Namespace==framename)
				{
					datas.splice(a,1);
					bool=true;
					a--;
				}
			}
		if(bool)
		{
		 egret.localStorage.removeItem("namespce");
		 egret.localStorage.setItem("namespace",JSON.stringify(datas));
		}
	}
	//储存obj数据;
	public savedataobjdata(framename:string)
	{
		if(!this.checknamespace(framename))
		{
           return false;
		}
		//写入命名空间
		this.writnamespace(framename);
		//拷贝角色数据
		let object=CBASE_MANAGE.getCBASE_MANAGE.getALLobject();
		let objdata:{}[]=[]
		for(let a=0;a<object.length;a++)
		{
           objdata.push(this.setobjSON(object[a]))
		}
		//拷贝刚体数据
		let bodydata=this.savedatabodydata();
		//拷贝脚本数据
		let scriptdata=this.savescriptdata();
		//拷贝舞台数据
		let plattedata=this.saveplattedata();
		//数据打包
		let datapackage:{}={}
		datapackage["objdata"]=objdata;
		datapackage["bodydata"]=bodydata;
		datapackage["scriptdata"]=scriptdata;
		datapackage["plattedata"]=plattedata;
		//存入本地
		egret.localStorage.setItem(framename,JSON.stringify(datapackage));
	
	}

	public setobjSON(OBJ:COBJECT):{}
	{
         let objdata:{}={}
		 objdata["name"]=OBJ.objName
		 objdata["x"]=OBJ.x
		 objdata["y"]=OBJ.y
		 objdata["rotation"]=OBJ.rotation
		 objdata["scaleX"]=OBJ.scaleX
		 objdata["scaleY"]=OBJ.scaleY
		 objdata["anchorOffsetX"]=OBJ.anchorOffsetX
		 objdata["anchorOffsetY"]=OBJ.anchorOffsetY
		 objdata["resname"]=OBJ.resname
         objdata["judge"]=this.judgeJSON(OBJ.judge)
		 objdata["unjudge"]=this.judgeJSON(OBJ.unjudge)
		 objdata["index"]=ASSIST.getASSIST.platte.getChildIndex(OBJ);
		 objdata["isbackground"]=OBJ.background?"1":"0";
		 return objdata
	}
	public  judgeJSON(judge:CCOLLISION[]):{}[]
	{
		let judagedata:{}[]=[]
        for(let a=0;a<judge.length;a++)
		 {
            
			 let judge1:{}={}
			 judge1["name"]=judge[a].name
			 judge1["judgetype"]=judge[a].judgetype
			 judge1["shapename"]=judge[a].shape.name;
			 if(judge[a].judgetype==judgetype.circle)
			 {
                  judge1["r"]=judge[a].r
			      judge1["x0"]=judge[a].shapepoint[0].x
				  judge1["y0"]=judge[a].shapepoint[0].y
			 }
			 else if(judge[a].judgetype==judgetype.rectangle)
			 {
			
			      judge1["x0"]=judge[a].shape.x
				  judge1["y0"]=judge[a].shape.y
				  judge1["width"]=(judge[a].shapepoint[1].x-judge[a].shapepoint[0].x)
				  judge1["height"]=(judge[a].shapepoint[2].y-judge[a].shapepoint[1].y)
			 }
			 else if(judge[a].judgetype==judgetype.poly)
			 {
				 judge1["pointnumber"]=judge[a].shapepoint.length
				 for(let b=0;b<judge[a].shapepoint.length;b++)
				 {
					  judge1["x"+b]=judge[a].shapepoint[b].x
					  judge1["y"+b]=judge[a].shapepoint[b].y
				 }
				
			 }
			 judagedata.push(judge1)
		 }
		 return judagedata
	}
    public savedatabodydata()
	{
		let data:{}[]=[]
		
		let massage=ASSIST.getASSIST.bodydata
		for(let a=0;a<massage.length;a++)
		{
		
			let bodydata:{}={}
			bodydata["parentname"]=massage[a].parent.objName;
			bodydata["bodyname"]=massage[a].name;
			data.push(bodydata);
		}
		return data;
	}
	public savescriptdata()
	{
		let data:{}[]=[]
		
		let massage=ASSIST.getASSIST.scriptdata
		for(let a=0;a<massage.length;a++)
		{
		
			let bodydata:{}={}
			bodydata["targetname"]=massage[a].target.objName
			bodydata["scriptname"]=massage[a].script
			
			data.push(bodydata);
		}
		return data;
	}
	public saveplattedata()
	{
		let massage=CMainEditor.editor.getplattedata()
			let plattedata:{}={}
			plattedata["plattewidth"]=massage[0]
			plattedata["platteheight"]=massage[1]
			plattedata["plattecolor"]=massage[2]
	
		return plattedata;
	}
	
    
}